package xid

import (
	"bytes"
)

const ErrorIdResult int64 = -1

type SnowFlakeModeConfig struct {
	Sname           string        `properties:"sname"`
	Namespace       string        `properties:"namespace"`
	Timeout         int64         `properties:"timeout"`
	Mode            SnowFlakeMode `properties:"mode"`
	TimeStampBitNum uint16        `properties:"timestampBitNum"`
	SequenceBitNum  uint16        `properties:"sequenceBitNum"`
}

func NewSnowFlakeModeConfig(sname, namespace string) SnowFlakeModeConfig {
	var cfg = SnowFlakeModeConfig{
		Sname:     sname,
		Namespace: namespace,
	}
	cfg.Init()
	return cfg
}

type SegmentModeConfig struct {
	ServName   string `properties:"servName"`
	Namespace  string `properties:"namespace"`
	Timeout    int64  `properties:"timeout"`
	DefaultQps int64  `properties:"defaultQps"`
}

func GetGeneratorKey(servName, namespace string) string {
	var buf bytes.Buffer
	if servName != "" {
		buf.WriteString(servName)
	}
	if namespace != "" {
		buf.WriteString(".")
		buf.WriteString(namespace)
	}
	return buf.String()
}

func (s *SnowFlakeModeConfig) GetKey() string {
	return GetGeneratorKey(s.Sname, s.Namespace)
}

func (s *SnowFlakeModeConfig) Init() error {
	if s.Mode == NORMAL_MOD {
		s.SequenceBitNum = DEFAULT_SEQUENCE_BIT_NUM
		s.TimeStampBitNum = DEFAULT_TIMESTAMP_BIT_NUM
		s.Timeout = DEFAULT_TIMEOUT
	}
	return nil
}

func (c *SegmentModeConfig) GetKey() string {
	return GetGeneratorKey(c.ServName, c.Namespace)
}
